<template>
  <div class="wrapper">
    <div style="width: 400px">
      <el-form :model="addForm" label-width="100px">
        <el-form-item label="学生名称">
          <el-input
            v-model="addForm.nickname"
            size="small"
            placeholder="请输入名称,最多可添加15个字"
          ></el-input>
        </el-form-item>
        <el-form-item label="学生手机号">
          <el-input
            size="small"
            v-model="addForm.mobile"
            placeholder="请输入手机号"
          ></el-input>
        </el-form-item>
        <el-form-item label="学员头像">
          <el-upload
            class="upload-demo"
            :action="uploadURL"
            :headers="headerObj"
            list-type="picture"
            :on-success="handleSuccess"
            :file-list="fileList"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件，且不超过500kb
            </div>
          </el-upload>
        </el-form-item>
        <!-- <el-form-item label="性别">
					<el-select v-model="addForm.sex" size="small" placeholder="请选择">
						<el-option :value="index" v-for="(item,index) in option" :key="index" :label="item.label">
						</el-option>
					</el-select>
				</el-form-item> -->
        <el-form-item label="出生日期">
          <el-col :span="11">
            <el-date-picker
              size="small"
              type="date"
              placeholder="选择日期"
              v-model="addForm.birthday"
              style="width: 100%"
            >
            </el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="所在城市">
          <el-select
            size="small"
            v-model="provinceLists"
            placeholder="请选择"
            @change="getCityList"
          >
            <el-option
              :value="item.id"
              v-for="item in provinceList"
              :key="item.area_code"
              :label="item.area_name"
            >
            </el-option>
          </el-select>

          <el-select
            size="small"
            v-model="cityLists"
            placeholder="请选择"
            @change="getDistrictList"
          >
            <el-option
              :value="item.id"
              v-for="item in cityList"
              :key="item.area_code"
              :label="item.area_name"
            >
            </el-option>
          </el-select>

          <el-select size="small" v-model="districtLists" placeholder="请选择">
            <el-option
              :value="item.id"
              v-for="item in districtList"
              :key="item.area_code"
              :label="item.area_name"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="small" @click="addUser('addFormRef')"
            >保存</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  // 组件名称
  name: "demo",
  // 组件参数 接收来自父组件的数据
  props: [],
  // 局部注册的组件
  components: {},
  // 组件状态值
  data() {
    return {
      // 添加用户的参数
      addForm: {
        avatar: "", //是	string	头像
        birthday: "", //是	string	生日日期
        province_id: this.provinceLists, //是	string	省ID
        city_id: this.cityLists, //string	城市ID
        district_id: this.districtLists, //是	string	地区ID
        mobile: "", //string	手机号码
        nickname: "", //是	string	姓名
        // sex: "",
        // // 上传的图片临时路径（对象）
        pics: [],
      },
      fileList: [],
      // 省
      provinceList: [],
      provinceLists: [],
      // 市
      cityList: [],
      cityLists: [],
      // 区
      districtList: [],
      districtLists: [],
      // 图片上传地址
      uploadURL: "http://120.53.31.103:84/api/public/img",
      // 图片上传组件的请求对象
      headerObj: {
        Authorization: "Bearer" + sessionStorage.getItem("school_token"),
      },
    };
  },
  mounted() {},
  // 计算属性
  computed: {},
  // 侦听器
  watch: {},
  // 组件方法
  methods: {
    handleSuccess(response) {
      console.log(response);
      let imageInfo = { pic: response.data.path };
      this.addForm.pics.push(imageInfo);
      console.log(this.addForm.pics);
    },

    async addUser() {
      try {
        const { data: res } = await this.$http.post("user", this.addForm);
        console.log(res);
        if (res.code !== 200) {
          return this.$message.error(res.msg);
        }
        this.$message.success(res.msg);
        this.$router.push("/Student");
      } catch (e) {
        console.log(e);
      }
    },
    // 获取城市的数据列表
    async getProvinceList() {
      try {
        const { data: res } = await this.$http.get("sonArea/0");
        console.log(res);
        this.provinceList = res.data;
      } catch (e) {
        this.$message.error(e);
      }
    },
    async getCityList(id) {
      try {
        const { data: res } = await this.$http.get("sonArea/" + id);
        console.log(res);
        this.cityList = res.data;
      } catch (e) {
        this.$message.error(e);
      }
    },
    async getDistrictList(id) {
      try {
        const { data: res } = await this.$http.get("sonArea/" + id);
        console.log(res);
        this.districtList = res.data;
      } catch (e) {
        this.$message.error(e);
      }
    },
  },
  /**
   * 组件实例创建完成
   */
  created() {
    this.getProvinceList();
  },
};
</script>

<style scoped>
.el-upload {
  width: 60px;
}
</style>

this.editForm = actIndex[this.total]; this.editForm.id = actIndex[this.total].uid;
this.fileList.url=actIndex[this.total].avatar;
